﻿using MTUnity.Utils;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.SocialPlatforms;
using UnityEngine.UI;

public class UI_Warehouse : MonoBehaviour
{
    public GameObject AssemblyLineCellTemplate;
    public GridLayoutGroup AssemblyLinePanel;
    public UI_Location_Title Panel_LocationBig;
    public UI_Location_Title Panel_LocationSmall;


    private List<UI_Location_Cell> m_listAllLocationCell = new List<UI_Location_Cell>();
    private Dictionary<int, UI_Location_Cell> m_dicID2LocationCell = new Dictionary<int, UI_Location_Cell>();
    private List<int> m_listShowInListLocationIDs = new List<int>();
    private int m_iBigTitleLocationID;

    FWarehouse m_warehouse;

    private FLocation m_CurrentLocation = null;

    // Start is called before the first frame update
    void Awake()
    {
        //GlobalEvent.AddEvent(EventName.OnLocationClicked, OnRecvChangeLocation);
        //GlobalEvent.AddEvent(EventName.LocationSelect, UpdateSelectLocation);

    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void SetWarehouse(FWarehouse warehouse)
    { 
        if (warehouse == null)
        {
            return;
        }
        m_warehouse = warehouse;
        
        //FLocation fullviewLocation = warehouse.GetLocationWithID(1);
        //FLocation fullviewPreviewLocation = warehouse.GetLocationWithID(fullviewLocation.GroupID);

        //List<int> deleteID = new List<int>() { 1, fullviewLocation.GroupID };


        //ReBuildListWithUnShowLocationID(deleteID);

        //ReSetBigAndSmallTitle(fullviewLocation, fullviewPreviewLocation);
    }

    public void SetLocation(int iID)
    {
        FLocation location = m_warehouse.GetLocationWithID(iID);

        if (m_CurrentLocation != null && m_CurrentLocation != location)
        {
            SetSelect(m_CurrentLocation.ID, false);
        }

        if (m_CurrentLocation == null || m_CurrentLocation.LocationType != location.LocationType)
        {
            SetLocationWithType(location.LocationType);
        }

        

        m_CurrentLocation = location;

        SetSelect(m_CurrentLocation.ID , true);

        SetBigTitle(location);
        SetSmallTitle(location);
    }

    public void SetLocationWithType(ELocationType locationType)
    {
        if (!ToolUtils.LocationCheck(locationType , ELocationType.E_Interior))
        {
            ELocationType bigType = ELocationType.E_None;
            switch (locationType)
            {
                case ELocationType.E_None:
                    break;
                case ELocationType.E_Interior:
                    bigType = ELocationType.E_Interior;
                    break;
                case ELocationType.E_Exterior:
                    bigType = ELocationType.E_Interior;
                    break;
                case ELocationType.E_Enter:
                case ELocationType.E_Exit:
                case ELocationType.E_EnterOrExit:
                    bigType = ELocationType.E_EnterOrExit;
                    break;
                case ELocationType.E_NormalSorting:
                case ELocationType.E_UnNormalSorting:
                case ELocationType.E_SortingLine:
                    bigType = locationType;
                    break;
                default:
                    break;
            }
            List<FLocation> list = m_warehouse.GetLocationWithType(bigType | ELocationType.E_Interior);
            BuildList(list);
        }
        else
        {
            BuildList(new List<FLocation>());
        }
        
        
    }

    public void SetSelect(int iID , bool bSelect)
    {
        if (m_dicID2LocationCell.ContainsKey(iID))
        {
            m_dicID2LocationCell[iID].SetSelect(bSelect);
        }
    }

    public void RefreshStatus()
    {
        foreach(var item in m_listAllLocationCell)
        {
            item.RefreshStatus();
        }
    }

    public void SetShowInList(int iBigLocation , int iSmallLocation , List<int> listLocationNeedShow)
    {
        FLocation bigLocation = m_warehouse.GetLocationWithID(iBigLocation);
        FLocation smallLocation = m_warehouse.GetLocationWithID(iSmallLocation);
        ReSetBigAndSmallTitle(bigLocation, smallLocation);
        //BuildListWithIDs(listLocationNeedShow);
    }

    public void ReBuildListWithUnShowLocationID(List<int> listRemoveLocationIDs)
    {
        List<int> temp = new List<int>(m_warehouse.GetAllLocationID());
        for (int i = 0; i < listRemoveLocationIDs.Count; i++)
        {
            temp.Remove(listRemoveLocationIDs[i]);
        }
        
        //BuildListWithIDs(temp);
    }

    public void BuildList(List<FLocation> list)
    {

        m_listShowInListLocationIDs.Clear();
        m_dicID2LocationCell.Clear();

        for (int i = 0; i < list.Count; i++)
        {
            UI_Location_Cell widget;
            if (i < m_listAllLocationCell.Count)
            {
                widget = m_listAllLocationCell[i];
                widget.gameObject.SetActive(true);
            }
            else
            {
                GameObject obj = GameObject.Instantiate(AssemblyLineCellTemplate) as GameObject;
                obj.transform.SetParent(AssemblyLinePanel.transform);
                obj.transform.localScale = Vector3.one;
                widget = obj.GetComponent<UI_Location_Cell>();
                m_listAllLocationCell.Add(widget);
            }
            FLocation location = list[i];
            widget.SetLocation(location);
            int iID = location.ID;
            widget.SetCallback(() => OnLocationClicked(iID));

            m_listShowInListLocationIDs.Add(iID);

            m_dicID2LocationCell.Add(iID, widget);

        }

        if (m_listShowInListLocationIDs.Count < m_listAllLocationCell.Count)
        {
            for (int i = m_listShowInListLocationIDs.Count; i < m_listAllLocationCell.Count; i++)
            {
                m_listAllLocationCell[i].gameObject.SetActive(false);
            }
        }
    }


    public void ReSetTitle(int iTitleLocationID)
    {
        ReSetTitle(m_warehouse.GetLocationWithID(iTitleLocationID));
    }

    public void ReSetTitle(FLocation location)
    {
        ReSetBigAndSmallTitle(location, m_warehouse.GetLocationWithID(location.GroupID));
    }
    public void ReSetBigAndSmallTitle(int iBig , int iSmall)
    {
        ReSetBigAndSmallTitle(m_warehouse.GetLocationWithID(iBig), m_warehouse.GetLocationWithID(iSmall));
    }

    public void ReSetBigAndSmallTitle(ELocationType eBig , ELocationType eSmall)
    {

    }

    public void SetBigTitle(FLocation location)
    {
        Panel_LocationBig.SetLocation(location , false);
    }

    public void SetSmallTitle(FLocation location)
    {
        Panel_LocationSmall.SetLocation(location);
    }

    public void ReSetBigAndSmallTitle(FLocation bigLocation , FLocation smallLocation)
    {
        m_iBigTitleLocationID = bigLocation.ID;
        Panel_LocationBig.SetLocation(bigLocation);
        Panel_LocationSmall.SetLocation(smallLocation);

        int iID = smallLocation.ID;
        Panel_LocationSmall.SetCallback(() => OnLocationClicked(iID));
      
    }

    private int SortList(int a , int b)
    {
        if (a > b)
        {
            return 1;
        }else if (a < b)
        {
            return -1;
        }
        else
        {
            return 0;
        }
    }

    void OnRecvChangeLocation(object data)
    {
        OnLocationClicked((int)data);
    }

    void OnLocationClicked(int iID)
    {
        GlobalEvent.DispatchEvent(EventName.LocationSelect, new Event_LocationSelect() { LocationID = iID });

        
    }

    void UpdateSelectLocation(object data)
    {
        Event_LocationSelect t = (Event_LocationSelect)data;
        SetLocation(t.LocationID);
    }
}
